#include<iostream>
#include <sys/time.h>
#include <cstring>
#include <chrono> 
#include <thread>
#include <unistd.h>
using namespace std;
using namespace chrono;

#include "../include/uart_transporter.hpp"

struct send_data
{
    int is_open;//是否打开灯带，0是关闭，1是打开                            
    int number;//代表灯板上的数字，0-9
    int color;
};

struct read_data
{
    int color;//选手识别到的颜色，1是红色，2是绿色
    int number;//选手识别到的数字 
};

UartTransporter uart_send("/dev/ttyACM0");
UartTransporter uart_read("/dev/ttyUSB0");


void read(read_data& data)
{
    //std::lock_guard<std::mutex> lock(mtx);
    uint8_t read_buffer[10];
   
    int bytesRead = uart_read.read(read_buffer, 10,100); // 留出空间给null字符 

    if (bytesRead > 0&&read_buffer[0]==0xA5) {  
        //read_buffer[bytesRead] = '\0'; // 确保字符串以null字符结尾  
        std::cout << "Read " << bytesRead << " bytes: " << read_buffer << std::endl; 
     
        memcpy(&data.number, read_buffer+1, sizeof(int));
        memcpy(&data.color, read_buffer+5, sizeof(int));
        printf("Retrieved data number: %d\n", data.number);
        printf("Retrieved data color: %d\n", data.color);  
    }
    else{
        std::cout<<"未能读取到数据"<<std::endl;
    }
           
};


void send(send_data& data)
{
    uint8_t buffer[14];
    buffer[0] = 0xA5;
    memcpy(buffer + 1, &data.is_open, sizeof(int));
    memcpy(buffer + 5, &data.number, sizeof(int));
    memcpy(buffer + 9, &data.color, sizeof(int));
    int is_write = uart_send.writeBuffer(buffer,14);
    if (is_write ==14 ) {  
        printf("Send data is_open: %d\n", data.is_open);
        printf("Send data number: %d\n", data.number);
        printf("Send data color: %d\n", data.color); 
    }

};

int main()
{   
    cout<<"进入系统初始化阶段"<<endl;


    cout<<"开始打开串口"<<endl;

label:
    bool is_uart_read_open = uart_read.open();
    bool is_uart_send_open = uart_send.open();
    if(!is_uart_read_open||!is_uart_send_open)
    {
        cout<<"串口打开失败，尝试打开串口"<<endl;
        goto label;
    }
    cout<<"串口打开成功"<<endl;
    


    cout<<"开始初始化赋值"<<endl;
    struct timeval time_start,time_end;
    double time = 0.0;
    int send_time = 0;
    send_data s_data;
    read_data r_data;
    send_data review_s_data;
    s_data.is_open = 0;
    s_data.color = 0;
    s_data.number = 0;
    send(s_data);
	
    int color_time = 1;//按顺序改变颜色
    s_data.is_open = 0;
    s_data.color = color_time;
    s_data.number = 4;
    review_s_data.is_open = s_data.is_open;
    review_s_data.color = s_data.color;
    review_s_data.number = s_data.number;
    r_data.color = -1;
    r_data.number = -1;
    int score = 0;//得分
    
    int detect_all_time = 0;//二者同时检测的次数
    int detect_color_time = 0;//仅仅检测颜色的次数
    bool is_open_road = false;//是否打开灯带
    bool q=true,w=true,e=true,r=true;
    cout<<"初始化赋值成功"<<endl;


    cout<<"-----等待用户输入，是否开始比赛(输入任意键开始比赛)-----"<<endl;
    string a;
    cin>>a;


    cout<<"-----计时开始-----"<<endl;
    auto start = system_clock::now();
    while(time<300)
    {
        if(is_open_road)
        {
            double time1 = 0;
            uart_read.close();
            auto start1 = system_clock::now();
            while(time1<45.0)
            {
                double time2;
                auto end1 = system_clock::now();
                auto duration1 = duration_cast<microseconds>(end1 - start1);
                auto duration2 = duration_cast<microseconds>(end1 - start);
                time1 = double(duration1.count()) * microseconds::period::num / microseconds::period::den;
                time2 = double(duration2.count()) * microseconds::period::num / microseconds::period::den;
                
                if(time2>=255)
                {
                    s_data.is_open = 2;
                    std::cout<<"最终阶段"<<endl;
                    send(s_data); 
                    sleep(1);
                    continue;
                }
                
                cout<<"比赛已经进行了"<<time2<<"s"<<endl;
                cout<<"通道还剩"<<45-time1<<"s"<<endl;
                sleep(1);
               

            }


            auto end = system_clock::now();
            auto duration = duration_cast<microseconds>(end - start);
            time = double(duration.count()) * microseconds::period::num / microseconds::period::den;
            s_data.is_open = 0;
            is_open_road = false;
            uart_read.open();
        }

        if(time<255)
        {
            //std::lock_guard<std::mutex> lock(mtx);
            cout<<"开始读取数据"<<endl;
            read(r_data);
            
            if(r_data.color==s_data.color&&r_data.number==s_data.number)
            {
                detect_all_time++;
            }
            else
            {
                detect_all_time = 0;
            }


            if(r_data.color==s_data.color)
            {
                detect_color_time++;
            }
            else{
                detect_color_time = 0;
            }
            

            if(detect_all_time>300)
            {
                score+=6;
                s_data.is_open = 1;
                detect_all_time = 0;
                detect_color_time = 0;
                is_open_road = true;
            }


            if(detect_color_time>400)
            {
                score+=2;
                s_data.is_open = 1;
                detect_color_time = 0;
                detect_all_time = 0;
                is_open_road = true;
            }


            if((time>60&&q)||(time>120&&w)||(time>180&&e)||(time>240&&r))
            {
                if(time>60&&time<120)
                {
                    q = false;
                }
                if(time>120&&time<180)
                {
                    q = false;
                    w = false;
                }
                if(time>180&&time<240)
                {
                    q = false;
                    w = false;
                    e = false;
                }
                if(time>240&&time<300)
                {
                    q = false;
                    w = false;
                    e = false;
                    r = false;
                }
                s_data.color = ++color_time;
                if(color_time==3)
                {
                	color_time =0;	    
		        }
                s_data.number = rand() % 8;
                std::cout<<"更改颜色以及数字"<<endl;

            }
            std::cout<<"成功识别颜色和数字的次数："<<detect_all_time<<std::endl;
            std::cout<<"成功识别颜色的次数:"<<detect_color_time<<std::endl;
            std::cout<<"目前得分为："<<score<<endl;
            std::cout<<"当前的send_data.color:"<<s_data.color<< std::endl;
            std::cout<<"当前的send_data.number:"<<s_data.number<<std::endl;
            std::cout<<"当前的review_data.color:"<<review_s_data.color<< std::endl;
            std::cout<<"当前的review_data.number:"<<review_s_data.number<<std::endl;
            

        }
        else
        {
            s_data.is_open = 2;
            std::cout<<"最终阶段"<<endl;
        }
        
        if(send_time==0||review_s_data.color!=s_data.color||review_s_data.is_open!=s_data.is_open||review_s_data.number!=s_data.number)
        {
            send(s_data);                                                                                                                
            review_s_data.color=s_data.color;
            review_s_data.is_open=s_data.is_open;
            review_s_data.number=s_data.number;
            send_time++;
        }
        auto end = system_clock::now();
        auto duration = duration_cast<microseconds>(end - start);
        time = double(duration.count()) * microseconds::period::num / microseconds::period::den;
        cout<<"比赛已经进行了"<<time<<"s"<<endl;
   
    }
    cout<<"最终的分为："<<score<<endl;


}
    



